﻿param($dpmServerName,$exportFilePath)

Import-Module -Force ..\BuildDeployment\libUtilities.psm1

$vtlRootPaths=Get-Content -Path .\VTL_RootPaths.txt

$dpmServerSession=Assert-PSSession -computerAddress $dpmServerName

$command={ 
            $dpmTapes=Get-ProtectionGroup | ForEach-Object {Get-Tape -ProtectionGroup $_} | Select-Object -Unique
            
            $dpmTapesInformation=@()
            
            #Credits to https://gallery.technet.microsoft.com/DPM-Tape-List-with-1f9fc697 for this portion
            foreach ($dpmTape in $dpmTapes) {
                
                Write-Host "Processing tape"$dpmTape.Barcode
                $Lower=$False
                $Action = ""
                $RPs=Get-RecoveryPoint -Tape $dpmTape
                $ExpDate = $RPs[0].recoverysourcelocations[0].expirydate
                foreach ($RP in $RPs) {
                    if ($ExpDate -lt $RP.recoverysourcelocations[0].expirydate) {
                        $ExpDate = $RP.recoverysourcelocations[0].expirydate  
                    } 
                }
                if ($ExpDate.Date -lt (Get-Date)) {$Lower=$True} 
                if ($dpmTape.Location.Id -eq 0) {$Slot = "Outside"} else {$Slot = $dpmTape.Location.Id}
                if ($Slot -eq "Outside" -and $Lower -eq $true) {$Action = "put Tape in Library"} 
                if ($Slot -ne "Outside" -and $ExpDate.Date -gt $OutsideDate -and $dpmTape.IsOffsiteReady -eq $True ) {$Action = "store Tape outside"} 
                $dpmTapeInformation = New-Object -TypeName PSObject
                $dpmTapeInformation | Add-Member Noteproperty  -Name ExpiryDate -value $ExpDate.Date
                $dpmTapeInformation | Add-Member Noteproperty -Name Tape -value $dpmTape.DisplayString
                $dpmTapeInformation | Add-Member Noteproperty -Name Slot -value $Slot
                $dpmTapeInformation | Add-Member Noteproperty -Name Barcode -value $dpmTape.Barcode.Value
                $dpmTapeInformation | Add-Member Noteproperty -Name Action -value $Action
                $dpmTapeInformation | Add-Member Noteproperty -Name LibraryName -value $dpmTape.LibraryName
                $dpmTapeInformation | Add-Member Noteproperty -Name ProtectionGroupName -value $dpmTape.ProtectionGroupName
                $dpmTapeInformation | Add-Member Noteproperty -Name MediaPoolType -value $dpmTape.MediaPoolType
                $dpmTapeInformation | Add-Member Noteproperty -Name DataWrittenDisplayString -value $dpmTape.DataWrittenDisplayString
                $dpmTapeInformation | Add-Member Noteproperty -Name CreationDate -value $dpmTape.CreationDate
                $dpmTapeInformation | Add-Member Noteproperty -Name ModifiedDate -value $dpmTape.ModifiedDate
                $dpmTapesInformation+=$dpmTapeInformation
            }

            $dpmTapesInformation

            }

$dpmTapesInformation=Invoke-Command -Session $dpmServerSession -ScriptBlock $command

foreach($vtlRootPath in $vtlRootPaths)
{
    $tapes=@()
    $tapes+=Get-ChildItem $vtlRootPath\*.fsrm -Recurse | Select FullName,Length,LastWriteTime

    
    foreach($tape in $tapes)
    {
        $matchedDPMTapes=($dpmTapesInformation | Where-Object {$_.Barcode -eq ($tape.FullName | Split-Path -Leaf).Replace(".fsrm","")})

        if($matchedDPMTapes -ne $null)
        {
            $matchedDPMTape=$matchedDPMTapes[0]
            $tapeExpiryDate=$matchedDPMTape.ExpiryDate
            $tapeLibraryName=$matchedDPMTape.LibraryName
            $tapeProtectionGroupName=$matchedDPMTape.ProtectionGroupName
            $tapeMediaPoolType=$matchedDPMTape.MediaPoolType
            $tapeCreationDate=$matchedDPMTape.CreationDate
            $tapeModifiedDate=$matchedDPMTape.ModifiedDate
        }

        $path=$tape.FullName;
        $libraryName=$tape.FullName.Split("\")[6];
        if($tape.FullName.Split("\")[6].Split("-").Count -eq 2)
        {
            $libraryType=$tape.FullName.Split("\")[6].Split("-")[0];
        }
        elseif($tape.FullName -ilike "*Deferrable*")
        {
            $libraryType="Deferrable"
        }
        elseif($tape.FullName -ilike "*Non-Critical*")
        {
            $libraryType="Non-Critical"
        }
        else
        {
            $libraryType="Other"    
        }
        $sizeInGB=$tape.Length/1GB;
        $lastWriteTime=$tape.LastWriteTime;
        $volumeName=$tape.FullName.Split("\")[3];
        $serverName=$tape.FullName.Split("\")[5];
        
        New-Object -TypeName PSObject -Property @{Path=$path;
                                                LibraryName=$libraryName;
                                                LibraryType=$libraryType;
                                                SizeInGB=$sizeInGB;
                                                LastWriteTime=$lastWriteTime;
                                                VolumeName=$volumeName;
                                                ServerName=$serverName;
                                                TapeExpiryDate=$tapeExpiryDate;
                                                TapeLibraryName=$tapeLibraryName;
                                                TapeProtectionGroupName=$tapeProtectionGroupName;
                                                TapeMediaPoolType=$tapeMediaPoolType;
                                                TapeCreationDate=$tapeCreationDate;
                                                TapeModifiedDate=$tapeModifiedDate;
                                                } | Export-Csv -Path $exportFilePath -Append -NoTypeInformation

    }

}

